<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Simple Carousel with Dots</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        .carousel-container {
            width: 100%;
            height: 400px;
            /* 设置高度 */
            overflow: hidden;
            position: relative;
            background-color: #f0f0f0;
            /* 设置背景颜色 */
        }

        .carousel-slide {
            display: flex;
            transition: transform 0.5s ease-in-out;
            height: 100%;
            /* 使滑动内容的高度与容器一致 */
        }

        .carousel-slide img {
            width: 100%;
            height: 100%;
            /* 使图片高度与滑动内容一致 */
            flex-shrink: 0;
            object-fit: cover;
            /* 保持图片比例并填充整个容器 */
        }

        .carousel-controls button {
            position: absolute;
            top: 50%;
            transform: translateY(-50%);
            background-color: rgba(0, 0, 0, 0.5);
            color: white;
            border: none;
            cursor: pointer;
            padding: 10px;
        }

        .prev {
            left: 10px;
        }

        .next {
            right: 10px;
        }

        .dots {
            display: flex;
            justify-content: center;
            position: absolute;
            bottom: 10px;
            width: 100%;
        }

        .dot {
            cursor: pointer;
            height: 10px;
            width: 10px;
            margin: 0 5px;
            background-color: #bbb;
            border-radius: 50%;
            transition: background-color 0.6s ease;
        }

        .active {
            background-color: #717171;
        }
    </style>
</head>

<body>

    <div class="carousel-container">
        <div class="carousel-slide" id="carousel-slide">
            <img src="image1.jpg" alt="Image 1">
            <img src="image2.jpg" alt="Image 2">
            <img src="image3.jpg" alt="Image 3">
        </div>
        <div class="carousel-controls">
            <button class="prev" onclick="moveSlide(-1)">❮</button>
            <button class="next" onclick="moveSlide(1)">❯</button>
        </div>
        <div class="dots" id="dots"></div>
    </div>

    <script>
        let slideIndex = 0;
        const slides = document.querySelectorAll('.carousel-slide img');
        const totalSlides = slides.length;
        const dotsContainer = document.getElementById('dots');

        // 初始化分页器
        function initDots() {
            for (let i = 0; i < totalSlides; i++) {
                const dot = document.createElement('div');
                dot.classList.add('dot');
                dot.addEventListener('click', () => {
                    moveSlide(i - slideIndex);
                });
                dotsContainer.appendChild(dot);
            }
            updateDots();
        }

        // 更新分页器状态
        function updateDots() {
            const dots = document.querySelectorAll('.dot');
            dots.forEach((dot, index) => {
                if (index === slideIndex) {
                    dot.classList.add('active');
                } else {
                    dot.classList.remove('active');
                }
            });
        }

        function moveSlide(step) {
            slideIndex += step;

            if (slideIndex >= totalSlides) {
                slideIndex = 0;
            } else if (slideIndex < 0) {
                slideIndex = totalSlides - 1;
            }

            const offset = -slideIndex * 100;
            document.getElementById('carousel-slide').style.transform = `translateX(${offset}%)`;
            updateDots();
        }

        // 初始化分页器
        initDots();

        // 自动播放
        setInterval(() => {
            moveSlide(1);
        }, 3000); // 每3秒自动切换到下一张图片
    </script>

</body>

</html>